package com.example.wanbaqian.activity;

import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.example.wanbaqian.R;
import com.example.wanbaqian.base.BaseActivity;
import com.example.wanbaqian.base.BaseSQLiteDao;
import com.example.wanbaqian.config.CommonValue;
import com.example.wanbaqian.base.HttpCallback;
import com.example.wanbaqian.config.HttpURL;
import com.example.wanbaqian.bean.ApplyOneBean;
import com.example.wanbaqian.bean.Info;
import com.example.wanbaqian.bean.UserInfoBean;
import com.example.wanbaqian.customView.BottomAreaDialog;
import com.example.wanbaqian.utils.BaseUtils;
import com.example.wanbaqian.utils.DialogUtils;
import com.example.wanbaqian.utils.GetJsonUtils;
import com.example.wanbaqian.utils.HttpUtils;
import com.example.wanbaqian.utils.MyCountDown;
import com.example.wanbaqian.utils.SPUtils;


import butterknife.BindView;
import butterknife.OnClick;
import chihane.jdaddressselector.OnAddressSelectedListener;
import chihane.jdaddressselector.model.City;
import chihane.jdaddressselector.model.County;
import chihane.jdaddressselector.model.Province;
import chihane.jdaddressselector.model.Street;

/**
 * 申请第一步
 */
public class ApplyOneActivity extends BaseActivity implements OnAddressSelectedListener, HttpCallback {
    private static final String TAG = "ApplyOneActivity";
    @BindView(R.id.apply_one_mobile)
    EditText applyOneMobile;
    @BindView(R.id.apply_one_code)
    EditText applyOneCode;
    @BindView(R.id.apply_one_getCode)
    TextView applyOneGetCode;
    @BindView(R.id.apply_one_position)
    TextView applyOnePosition;
    @BindView(R.id.apply_one_address)
    EditText applyOneAddress;
    @BindView(R.id.apply_one_Marriage)
    TextView applyOneMarriage;
    @BindView(R.id.apply_one_live)
    TextView applyOneLive;
    @BindView(R.id.apply_one_email)
    EditText applyOneEmail;
    @BindView(R.id.apply_one_ok)
    Button mTure;

    public static Activity instence;

    private BottomAreaDialog dialog;
    BaseSQLiteDao dao;
    private int type;
    private String mobile, address, email;
    private UserInfoBean userInfo;


    @Override
    protected int getLayout() {
        return R.layout.activity_apply_one;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        instence = this;
        dao = new BaseSQLiteDao(mContext);
        setStatusBarColor(R.color.main_color);
        type = getIntent().getIntExtra("type", 0);
        if (type == 0) {
            setToolBar("我的资料");
            mTure.setText("保存");

        } else if (type == 1) {
            setToolBar("额度申请");
            mTure.setText(" 下一步");
        }
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        if (null != getIntent().getExtras() && null != getIntent().getExtras().getSerializable("bean")) {
            initDateView((ApplyOneBean) getIntent().getExtras().getSerializable("bean"));
        }
    }

    @Override
    protected void initDate() {
        userInfo = SPUtils.getUserInfo(mContext);
        applyOneMobile.setText(userInfo.getLogin_mobile());
        applyOneMobile.setKeyListener(null);
        HttpUtils.getInstance().post(GetJsonUtils.getInstance().getRequest(HttpURL.LANMEI_ENUMS, mContext,
                new String[]{"accessid"}, new String[]{SPUtils.getAccessid(mContext)}), HttpURL.LANMEI_ENUMS, CommonValue.REQUEST_CODE2, this);
        dialog = new BottomAreaDialog(mContext);
        dialog.setOnAddressSelectedListener(this);
        dialog.getCity();
    }

    @Override
    public void onAddressSelected(Province province, City city, County county, Street street) {
        applyOnePosition.setText(province.name + city.name + county.name);
        applyOnePosition.setTag(county.id);
        dialog.dismiss();
    }

    @OnClick({R.id.apply_one_getCode, R.id.apply_one_position, R.id.apply_one_Marriage, R.id.apply_one_live, R.id.apply_one_ok})
    public void onClick(View view) {
        mobile = applyOneMobile.getText().toString();
        address = applyOneAddress.getText().toString();
        email = applyOneEmail.getText().toString();
        switch (view.getId()) {
            case R.id.apply_one_getCode:
                if (!BaseUtils.isMobileNO(mobile)) {
                    showToast("请填写正确的手机号码");
                    return;
                } else {
                    HttpUtils.getInstance().post(
                            GetJsonUtils.getInstance().getRequest(HttpURL.SMS_SEND, mContext,
                                    new String[]{"accessid", "user_mobile", "sms_type"},
                                    new String[]{SPUtils.getAccessid(mContext), mobile, "2"}),
                            mContext, "", CommonValue.REQUEST_CODE1, this);
                }
                break;
            case R.id.apply_one_position:
                dialog.show();
                break;
            case R.id.apply_one_Marriage:
                DialogUtils.showSingDialog("请选择婚姻情况", "marital", mContext, new DialogUtils.DialogInterface() {
                    @Override
                    public void backValue(String key, String value) {
                        applyOneMarriage.setText(value);
                        applyOneMarriage.setTag(key);
                    }
                });
                break;
            case R.id.apply_one_live:
                DialogUtils.showSingDialog("请选择居住时长", "living", mContext, new DialogUtils.DialogInterface() {
                    @Override
                    public void backValue(String key, String value) {
                        applyOneLive.setText(value);
                        applyOneLive.setTag(key);
                    }
                });
                break;
            case R.id.apply_one_ok:
//                openActivity(ApplyTwoActivity.class);
//                if (!BaseUtils.isMobileNO(mobile)) {
//                    showToast("请填写正确的手机号码");
//                    return;
//                }
//                if (TextUtils.isEmpty(code) || TextUtils.isEmpty(address) || TextUtils.isEmpty(email)
                if (TextUtils.isEmpty(address) || TextUtils.isEmpty(email) || null == applyOneLive.getTag() || null == applyOneMarriage.getTag() || null == applyOnePosition.getTag()) {
                    showToast("请填写正确的信息");
                    return;
                } else {
                    //TODO 加上邮箱验证，需要大量测试

                    boolean digitsOnly = TextUtils.isDigitsOnly(address);
                    Log.e(TAG, "on是否是纯数字Click: "+digitsOnly);
                    if(digitsOnly){
                        showToast("请填写正确的住址");
                        return;
                    }


                    if(!BaseUtils.isEmail(email)){
                        showToast("请填写正确的邮箱地址");
                        return;
                    }



                    if (type == 1) {
                        HttpUtils.getInstance().post(
                                GetJsonUtils.getInstance().getRequest(HttpURL.AUTH_APPLY, mContext,
                                        new String[]{"accessid", "loan_mobile", "living_area_no",
                                                "living_address", "living_times", "marital_status", "user_email"},
                                        new String[]{SPUtils.getAccessid(mContext), mobile, applyOnePosition.getTag() + "",
                                                address, applyOneLive.getTag() + "", applyOneMarriage.getTag() + "", this.email}),
                                mContext, "", CommonValue.REQUEST_CODE3, this);
                    } else if (type == 0) {
                        HttpUtils.getInstance().post(
                                GetJsonUtils.getInstance().getRequest(HttpURL.AUTH_APPLY, mContext,
                                        new String[]{"accessid", "loan_mobile", "living_area_no",
                                                "living_address", "living_times", "marital_status", "user_email"},
                                        new String[]{SPUtils.getAccessid(mContext), mobile, applyOnePosition.getTag() + "",
                                                address, applyOneLive.getTag() + "", applyOneMarriage.getTag() + "", this.email}),
                                mContext, "", CommonValue.REQUEST_CODE4, this);
                    }
                }
                break;
//            case R.id.apply_two_peopleLayout:
//                AuthBuilder mAuthBuidler = new AuthBuilder("1354321032456123", CommonValue.TDAUTHKEY, "", this);
//                mAuthBuidler.faceAuth(this);
//                break;
//            case R.id.apply_two_bankLayout:
//                if (apply_one_user.getText().toString().equals("身份认证(已认证)")) {
//                    openActivity(FindEdiBankActivity.class);
//                } else {
//                    showToast("请先实名认证");
//                }
//                break;
        }
    }

    @Override
    public void onSuccess(String content, Info info, int requestCode) {
        switch (requestCode) {
            case CommonValue.REQUEST_CODE1:
                showToast("验证码发送成功");
                new MyCountDown(60 * 1000, 1000, applyOneGetCode).start();
                break;
            case CommonValue.REQUEST_CODE2:
                dao.createEnum(content, "income")
                        .createEnum(content, "marital")
                        .createEnum(content, "induction")
                        .createEnum(content, "living")
                        .createEnum(content, "relation");
                break;
            case CommonValue.REQUEST_CODE3:
                openActivity(ApplyTwoActivity.class);
                break;
            case CommonValue.REQUEST_CODE4:
                showToast("保存成功!");
                finish();
                break;
//            case CommonValue.REQUEST_CODE4:
//                showToast("身份验证成功");
//                apply_one_user.setText("身份认证(已认证)");
//                break;
//            case CommonValue.REQUEST_CODE5:
            //填过资料的用户的信息填充
//                ApplyOneBean applyOneBean = JsonUtils.getInstance().jsonToObjectk(content, ApplyOneBean.class);
//                if (null != applyOneBean) {
//                    try {
//                    apply_one_user.setText(applyOneBean.getRealname_fill_flg() == 0 ? "身份认证" : "身份认证(已认证)");
//                    apply_one_bank.setText(applyOneBean.getBank_fill_flg() == 0 ? "添加银行卡" : "添加银行卡(已添加)");
//                        initDateView(applyOneBean);
//                    } catch (Exception e) {
//                        e.printStackTrace();
//                    }
//                }
//                break;
        }
    }

    /**
     * 填充之前填写的信息
     *
     * @param applyOneBean
     */
    private void initDateView(ApplyOneBean applyOneBean) {
        try {
            applyOneMobile.setText(BaseUtils.isNull(applyOneBean.getLoan_mobile()));
            applyOnePosition.setText(BaseUtils.isNull(applyOneBean.getLiving_area_no_str()));
            applyOnePosition.setTag(applyOneBean.getLiving_area_no());

            applyOneMarriage.setText(BaseUtils.isNull(applyOneBean.getMarital_status_str()));
            applyOneMarriage.setTag(applyOneBean.getMarital_status());

            applyOneLive.setText(BaseUtils.isNull(applyOneBean.getLiving_times_str()));
            applyOneLive.setTag(applyOneBean.getLiving_times());
            applyOneEmail.setText(BaseUtils.isNull(applyOneBean.getUser_email()));
            applyOneAddress.setText(BaseUtils.isNull(applyOneBean.getLiving_address()));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onCacheSuccess(String content, Info info, int requestCode) {

    }

    @Override
    public void onError(Info info, int requestCode) {
        showToast(info.getMsg());
    }


//    @Override
//    public void onResult(String s) {
//        if (!JsonUtils.getInstance().getJsonObject(s, "ret_code").equals("900001")) {
//            HttpUtils.getInstance().post(GetJsonUtils.getInstance().getRequest(HttpURL.THIRD_RESPONSE, mContext,
//                    new String[]{"accessid", "response_type", "response_info"},
//                    new String[]{SPUtils.getAccessid(mContext), CommonValue.realname, s}),
//                    mContext, "", CommonValue.REQUEST_CODE4, this);
//        }
//
//    }
}
